<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="root" :x="n">
    <h2>当前的n值是:{{n}}</h2>
    <button @click="add">点我n+1</button>
    <button @click="BYE">点我销毁vm</button>
  </div>

  <script>
    const vm = new Vue({
      el: '#root',
      data: {
        n: 1,
      },
      methods: {
        add() {
          console.log('add函数被调用');
          this.n++;
        },
        BYE() {
          console.log('调用了BYE函数');
          this.$destroy();
        },
      },
      watch: {
        n() {
          console.log('n的值发生了变化');
        },
      },
      beforeCreate() {
        console.log('beforeCreate');
        console.log(this, 'n的值', this.n);
        debugger;
      },
      created() {
        console.log('created');
        console.log(this, 'n的值', this.n);
        debugger;
      },
      beforeMount() {
        console.log('beforeMount');
        debugger;
      },
      // 完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted
      mounted() {
        console.log('mounted');
        console.log(this.$el);
        debugger;
      },
      // data中的数据发生改变时，会触发下面两个生命周期钩子
      beforeUpdate() {
        // 数据与页面未同步 数据是新的
        console.log(this.n);
      },
      updated() {
        // 数据与页面保持同步
        console.log(this.n);
        debugger;
      },
      beforeDestroy() {
        console.log('beforeDestroy');
        console.log(this.n);
        this.add(); // 函数执行，但页面未更新
        this.n = 99;
      },
      destroyed() {
        // 销毁完毕
      },
    });
  </script>
</body>

</html>